package com.jsj.service.impl;

import com.jsj.mapper.BorrowReturnMapper;
import com.jsj.mapper.DeviceMapper;
import com.jsj.pojo.Borrow;
import com.jsj.service.BorrowReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BorrowReturnServiceImpl implements BorrowReturnService {

@Autowired
    BorrowReturnMapper borrowReturnMapper;

@Autowired
    DeviceMapper deviceMapper;

    @Override
    public List<Borrow> queryAllBorrow() {
        List<Borrow> borrows = borrowReturnMapper.queryAllBorrow();
        return borrows;
    }

    @Override
    public List<Borrow> queryBorrowByStatus(String borrowStatus) {
        List<Borrow> borrows = borrowReturnMapper.queryBorrowByStatus(borrowStatus);

        return borrows;
    }

    //关键字搜索
    @Override
    public List<Borrow> queryBorrowByName(String deviceName) {

        List<Borrow> borrows = borrowReturnMapper.queryBorrowByName(deviceName);

        return borrows;
    }
//增加借出
    @Override
    public Boolean addBorrow(Borrow borrow) {
        //将借出设备状态更新为已借出0
        String deviceStatus="已借出";
        Boolean aBoolean = deviceMapper.updateDeviceStatus(borrow.getDeviceId(), deviceStatus);
        //将本设备状态改为，未归还0
        borrow.setBorrowStatus("未归还");
        //将借出信息存入借出表
        Boolean flag = borrowReturnMapper.addBorrow(borrow);

        if (aBoolean=true){
            if (flag=true){
                return true;
            }
        }
        return false;
    }
//删除借出信息
    @Override
    public Boolean returnDevice(int borrowId, int deviceId, Date returnTime) {
        //更改deviceStatus状态为正常1
        String deviceStatus="正常";
        Boolean aBoolean = deviceMapper.updateDeviceStatus(deviceId,deviceStatus);
        //更改借出表状态为已归还1
        String borrowStatus="已归还";

        Boolean flag = borrowReturnMapper.returnDevice(borrowId,returnTime,borrowStatus);
        if (aBoolean=true){
            if (flag=true){
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean updateBorrowById(Borrow borrow) {
        Boolean flag = borrowReturnMapper.updateBorrowById(borrow);

        return flag;
    }

    @Override
    public Boolean delBorrowById(int borrowId) {
        Boolean flag = borrowReturnMapper.delBorrowById(borrowId);
        return flag;
    }


}
